<?php


namespace app\backstage\controller;


use app\backstage\logic\RechargeLogic;
use think\Controller;

class Banner extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    /**
     * 批量设置banner表单
     * @return mixed
     * @Author: whh
     */
    public function plAddNovel(){
        if (request()->isAjax()){
            $params = request()->param();
            $result = $this->validate($params, 'app\backstage\validate\BannerValidate.plAddNovel');
            if (true !== $result) {
                $this->error($result);
            }
            $data = [];
            for ($i = 1;$i <= 4;$i++){
                $data[$i]['nidArr'] = input("table_novel_id{$i}/a",[]);
                $data[$i]['picArr'] = input("pic{$i}/a",[]);
                if (count($data[$i]['nidArr']) != count($data[$i]['picArr'])){
                    $this->error("保存失败，位置 {$i} 小说数量和banner数量不一致");
                }
                if (!$data[$i]['nidArr']){
                    unset($data[$i]);
                }
            }
            $save = [];
            $qas = explode(',',$params['agent_id']);
            foreach ($qas as $quid) {
                foreach ($params['type'] as $type){
                    foreach ($data as $i => $item){
                        foreach ($item['nidArr'] as $k => $nid){
                            $save[] = [
                                'agent_id' => $quid,
                                'seat' => $i - 1,//数据库位置从0开始
                                'type' => $type,
                                'content_type' => 1,
                                'novel_id' => $nid,
                                'banner' => $item['picArr'][$k]
                            ];
                        }
                    }
                }
            }
            $model = new \app\common\model\Banner();
            $model->insertAll($save);
            $this->success();
        }else {
            $user = request()->_user;
            $agents = getAgents();

            $agent_list[] = ['name'=>'分销平台','value'=>'0','selected'=>false];
            foreach ($agents as $k => $v) {
                $agent_list[] = [
                    'name'      => $v['account'],
                    'value'     => $v['id'],
                    'selected'  => false
                ];
            }

            $this->assign('agent_list', json_encode($agent_list, JSON_UNESCAPED_UNICODE));
            return $this->fetch();
        }
    }
}